import urllib2
import socket
import sys
from HttpProxyReader import HttpProxyReader

def renew_identity():
	success = False
	s = socket.socket()
	s.connect(('localhost', 9051))
	s.send("AUTHENTICATE\r\n")
	resp = s.recv(1024)
	if resp.startswith('250'):
		s.send("signal NEWNYM\r\n")
		resp = s.recv(1024)
	if resp.startswith('250'):
		success = True
	return success

httpProxyReader = HttpProxyReader()
while True:
	if httpProxyReader.renewIdentity():
	#print httpProxyReader.read('http://torcheck.xenobite.eu/')
		print httpProxyReader.read('http://maps.googleapis.com/maps/api/directions/json?origin=48.174287%2C17.04909&destination=48.17482%2C17.048955&mode=walking&language=sk&region=sk&sensor=false')
sys.exit(0)


if renew_identity():
	proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"})
	opener = urllib2.build_opener(proxy_support) 
	opener.addheaders = [('User-agent', 'Mozilla/5.0')]
	print opener.open('http://torcheck.xenobite.eu/').read()
	 	
